/* Copyright 2010 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT c;pWARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * @author Jeremy Brown
 */

package org.ethnologue.iso639_3;
import org.sil.iso639_3.*;
import java.util.Locale;

public class ELD /*Ethnologue Language Data*/
{
	//Language Status
	public static final String LIVING = "L";
	public static final String SECOND_LANGUAGE_ONLY = "S";
	public static final String NEARLY_EXTINCT = "N";
	public static final String EXTINCT = "X";
	
	//Language Name Type
	public static final String LANGUAGE = "L";
	public static final String LANGUAGE_ALTERNATE = "LA";
	public static final String LANGUAGE_PEJORATIVE = "LP";
	public static final String DIALECT = "D";
	public static final String DIALECT_ALTERNATE = "DA";
	public static final String DIALECT_PEJORATIVE = "DP";
	
	private String code; //ISO639-3 language code (3 letters, lower case)
	private String name; //ISO639-3 language name
	private String country; //Country of origin or primary country where spoken
	private String languageStatus; //Current status 
	private String nameType;
	private String[] countries; //List of country codes where the language is spoken
	
	private boolean retrievedLD = false;
	private LD ld = null; //Stores the LD (SIL/ISO639-3 Language Data) for this language code (if available). This is lazily resolved by the getLD() method.
	
	public ELD(String name)
	{
		this(name, LIVING, LANGUAGE);
	}
	
	public ELD(String name, String nameType)
	{
		this(name, LIVING, nameType);
	}

	public ELD(String name, String languageStatus, String nameType)
	{
		this("", name, "", new String[]{""}, languageStatus, nameType);
	}
	
	public ELD(String code, String name, String country, String languageStatus)
	{
		this(code, name, country, new String[]{country}, languageStatus, LANGUAGE);
	}
	
	public ELD(String code, String name, String country, String languageStatus, String nameType)
	{
		this(code, name, country, new String[]{country}, languageStatus, nameType);
	}
	
	public ELD(String code, String name, String country, String[] countries, String languageStatus)
	{
		this(code, name, country, countries, languageStatus, LIVING);
	}
	
	public ELD(String code, String name, String country, String[] countries, String languageStatus, String nameType)
	{
		this.code=code;
		this.name=name;
		this.country=country;
		this.languageStatus=languageStatus;
		this.nameType=nameType;
		this.countries = countries;
	}

	
	public void setCode(String code){this.code = code;}
	public void setCountry(String country){this.country = country;}
	public void setName(String name){this.name = name;}
	public void setLanguageStatus(String languageStatus){this.languageStatus = languageStatus;}
	public void setNameType(String nameType){this.nameType = nameType;}
	public void setCountries(String[] countries){this.countries = countries;}
	
	public String getCode(){return code;}
	public String getName(){return name;}
	public String getCountry(){return country;}
	public String[] getCountries(){return countries;}
	public String getLanguageStatus(){return languageStatus;}
	public String getNameType(){return nameType;}

	public String getDisplayCountry(){return getDisplayCountry(Locale.getDefault());}
	public String getDisplayCountry(Locale loc){return (new Locale("", country)).getDisplayCountry(loc);}
	public static String getDisplayCountry(String c)
	{
		return ELD.getDisplayCountry(c, Locale.getDefault());
	}
	public static String getDisplayCountry(String c, Locale loc)
	{
		return (new Locale("", c)).getDisplayCountry(loc);
	}
	
	public String[] getDisplayCountries()
	{
		return getDisplayCountries(Locale.getDefault());
	}
	public String[] getDisplayCountries(Locale loc)
	{
		String[] cNames = new String[countries.length];
		int i = 0;
		for(String c : countries)
		{
			cNames[i] = ELD.getDisplayCountry(c, loc);
			i++;
		}
		return cNames;
	}
	
	public String getDisplayLanguageStatus()
	{
		return ELD.getDisplayLanguageStatus(languageStatus);
	}
	public static String getDisplayLanguageStatus(String s)
	{
		if(s.equals(LIVING)){return "Living";}
		if(s.equals(SECOND_LANGUAGE_ONLY)){return "Second Language Only";}
		if(s.equals(NEARLY_EXTINCT)){return "Nearly Extinct";}
		if(s.equals(EXTINCT)){return "Extinct";}
		return s;
	}
	
	public String getDisplayNameType()
	{
		return ELD.getDisplayNameType(nameType);
	}
	public static String getDisplayNameType(String t)
	{
		if(t.equals(LANGUAGE)){return "Language Primary Name";}
		if(t.equals(LANGUAGE_ALTERNATE)){return "Language Alternate Name";}
		if(t.equals(LANGUAGE_PEJORATIVE)){return "Language Pejorative Name";}
		if(t.equals(DIALECT)){return "Dialect Primary Name";}
		if(t.equals(DIALECT_ALTERNATE)){return "Dialect Alternate Name";}
		if(t.equals(DIALECT_PEJORATIVE)){return "Dialect Pejorative Name";}
		return t;
	}
	
	public LD getLD()
	{
		if(!retrievedLD)
		{
			ld = LanguageByCode.get(code);
			retrievedLD = true;
		}
		return ld;
	}

	
	public String toString()
	{
		return code + "_" + country + "_" + name;
	}
}